package com.mall.service.commodity.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonPage;
import com.mall.commons.exception.CustomException;
import com.mall.model.domain.PmsProductAttribute;
import com.mall.model.domain.PmsProductAttributeCategory;
import com.mall.model.vo.PmsProductAtteibuteVo;
import com.mall.service.commodity.mapper.ProductAttributeMapper;
import com.mall.service.commodity.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ProductAttributeService
 * @Description 商品类型逻辑层
 * @Author dcq
 * @Date 2023/3/12 9:31
 * @Version 1.0
 */
@Service
public class ProductAttributeCateService {

    @Autowired
    private ProductAttributeMapper productAttributeMapper;
    @Autowired
    private ProductMapper productMapper;
    //商品属性分类
    public CommonPage listSei(Integer pageNum, Integer pageSize) {
        Page<PmsProductAttributeCategory> myPage = new Page<>(pageNum, pageSize);
        Integer integer = productAttributeMapper.selectCount(null);
        Page<PmsProductAttributeCategory> selectPage = productAttributeMapper.selectPage(myPage, null);
        CommonPage page = CommonPage.getPage(selectPage);
        return page;
    }
    //增加属性分类
    public void createSei(String name) {
        PmsProductAttributeCategory pmsProductAttributeCategory = new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setName(name);
        pmsProductAttributeCategory.setAttributeCount(0);
        pmsProductAttributeCategory.setParamCount(0);
        productAttributeMapper.insert(pmsProductAttributeCategory);
        Long id = pmsProductAttributeCategory.getId();
        if (id==null){
            throw new CustomException("增加失败");
        }
    }
   //删除单个属性分类
    public void deleteSei(Integer id) {
        productAttributeMapper.deleteById(id);
    }
    //修改单个属性分类
    public void updateSei(Integer id, String name) {
        PmsProductAttributeCategory pmsProductAttributeCategory = productAttributeMapper.selectById(id);
        pmsProductAttributeCategory.setName(name);
        productAttributeMapper.updateById(pmsProductAttributeCategory);
    }
    //获取所有商品属性分类及其下属性
    public List<PmsProductAtteibuteVo> withAttrSei() {
        //所有商品属性
        List<PmsProductAttributeCategory> pmsProductAttributeCategories = productAttributeMapper.selectList(null);
        //属性id
        List<Long> ids = CollUtil.getFieldValues(pmsProductAttributeCategories, "id", Long.class);
        //条件构造器
        Map map=new HashMap();
        for (Long id : ids) {
            LambdaQueryWrapper<PmsProductAttribute>queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(id!=null,PmsProductAttribute::getProductAttributeCategoryId,id);
            List<PmsProductAttribute> pmsProductAttributes = productMapper.selectList(queryWrapper);
            map.put(id,pmsProductAttributes);
        }
        List<PmsProductAtteibuteVo> collect = pmsProductAttributeCategories.stream().map((item) -> {
            PmsProductAtteibuteVo vo = new PmsProductAtteibuteVo();
            BeanUtils.copyProperties(item, vo);
            vo.setId(Convert.toInt(item.getId()));
            List<PmsProductAttribute> pmsProductAttributes = (List<PmsProductAttribute>) map.get(item.getId());
            vo.setProductAttributeList(pmsProductAttributes);
            return vo;
        }).collect(Collectors.toList());

       return collect;
    }
   //获取单个商品属性分类信息
    public PmsProductAttributeCategory getIdSei(Integer id) {
        PmsProductAttributeCategory pmsProductAttributeCategory = productAttributeMapper.selectById(id);
        return pmsProductAttributeCategory;
    }

}